﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using System.Data.SqlClient;

namespace MyNet.WebServices {

    using MyNet.WebServices.DataModel;
    using MyNet.WebServices.DataProviders.Facebook;


    public enum RegularFacebookUser { Javier, Genoveva, Epal }

    [ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
    public abstract class MyNetDbService : IMyNetDbService {

        
        protected List<Contact> myNetDbContacts = new List<Contact>();
        protected int nextContactId = 0;
        protected int nextAddressId = 0;


        public void addFacebookUser( RegularFacebookUser facebookUser, String accessToken ) {

            switch ( facebookUser ) { 
            
                case RegularFacebookUser.Javier :
                    this.addFacebookUser( "javiera.espinosa", accessToken );
                    break;

                case RegularFacebookUser.Genoveva :
                    this.addFacebookUser( "genoveva.vargas", accessToken );
                    break;

                case RegularFacebookUser.Epal :
                    this.addFacebookUser( "epalorleant", accessToken );
                    break;
            
            }

        } // method


        /// Insert Facebook User and his/her Friends into (pseudo) MyNetDB
        public void addFacebookUser( string facebookUser, string accessToken ) {

            ///
            // Import Facebook User Profile and Friends
            ///

            FacebookClient facebook = new FacebookClient();
            FacebookUser   fbUser = facebook.getUser( facebookUser, accessToken );
            List<FacebookUser> fbUserFriends = facebook.getUserFriends( facebookUser, accessToken );

            ///
            // Prepare MyNet Contact object with Facebook User information
            ///

            // Transform FacebookUser object into Contact object
            Contact contact = facebookUserToContact( fbUser );

            // Convert FacebookUser Friends into Contact objects
            foreach ( var friend in fbUserFriends )
                contact.Contacts.Add( facebookUserToContact( friend ) );

            // Insert Contacts into (pseudo) MyNetDB
            myNetDbContacts.Add( contact );

            foreach ( var c in contact.Contacts )
                myNetDbContacts.Add( c );

        } // Method


        /// Get All Contacts in MyNetDB
        public List<Contact> getContacts() {

            return myNetDbContacts;
                    
        } // Method

        /// Transforms a FacebookUser to a Contact object
        public Contact facebookUserToContact( FacebookUser facebookUser ) {

            // Contact object with partial information of a Facebook User 
            var contact = new Contact {
                Id = nextContactId++,
                Firstname = facebookUser.first_name,
                Lastname = facebookUser.last_name,
                SocialNetworkBasicInfo = new SocialNetworkBasicInfo {
                    SocialNetworkID = facebookUser.id,
                    WebSite = facebookUser.website,
                    Languages = new List<SocialNetworkLanguage>(),
                    EMails = new List<EMail>(),
                    Addresses = new List<Address>()
                },
                Contacts = new List<Contact>()
            };


            // User specified his/her spoken Languages ?
            if ( facebookUser.languages != null ) {

                // Add Facebook Languages to the Contact object
                foreach ( var fbLanguage in facebookUser.languages ) {

                    // Convert FacebookLanguage to SocialNetworkLanguage
                    contact.SocialNetworkBasicInfo.Languages.Add(
                        new SocialNetworkLanguage {
                            Id = long.Parse( fbLanguage.id ),
                            Name = fbLanguage.name
                        }
                    );

                } // For

            } // If


            // Assume Facebook User has 1 Personal mail (maximum)
            contact.SocialNetworkBasicInfo.EMails.Add(
                new EMail {
                    Address = facebookUser.email,
                    Type = EMailType.Personal
                }
            );

            ///
            // Assume that Facebook Users do not specify their Addresses
            ///

            return contact;

        } // Method




    } // Class   

}
